package com.infore.siteManagement.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.dto.StationDto;
import com.infore.siteManagement.dto.StationInfoDto;
import com.infore.siteManagement.dto.StatisDataQueryVO;
import com.infore.siteManagement.dto.YunlAppInfoParam;
import com.infore.siteManagement.entity.Station;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.PageObject;
import com.infore.siteManagement.service.StationService;
import com.infore.siteManagement.util.CookiesUtil;
import com.infore.siteManagement.util.ResultEntity;
import com.infore.siteManagement.vo.MainStatisVO;
import com.infore.siteManagement.vo.StationTypeListVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "站点管理接口", tags = { "站点管理接口" })
@RestController
@RequestMapping("/station")
public class StationController {
	@Autowired
	private StationService stationService;

	@ApiOperation(value = "国----编辑/新增站点接口")
	@ResponseBody
	@PostMapping(value = "/editOrAdd")
	public ResultEntity editOrAddStation(@RequestBody StationInfoDto stationInfoDto, HttpServletRequest request) {
		String userId = CookiesUtil.getCookieValue(request, "userId");
		if (StringUtils.isNotBlank(userId)) {
			stationInfoDto.setUserId(userId);
		}
		return stationService.editOrAddStation(stationInfoDto);
	}

	@ApiOperation(value = "编辑/新增站点信息接口")
	@ResponseBody
	@PostMapping(value = "/editOrAddStation")
	public ResultEntity editOrAddStation(@RequestBody StationDto stationDto) {
		return stationService.editOrAddStation(stationDto);
	}

	// 根据地区和名称查询
	@ApiOperation(value = "国----监测站信息查询接口")
	@ResponseBody
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaId", value = "区域id", paramType = "query", required = false, dataType = "String"),
			@ApiImplicitParam(name = "stationName", value = "站点名称", paramType = "query", required = false, dataType = "String") })
	@GetMapping(value = "/singleStationInfo")
	public ResultEntity singleStationInfo(String areaId, String stationName, PageObject page,
			HttpServletRequest request) {
		return stationService.singleStationInfo(areaId, stationName, page, request);
	}

	@ApiOperation(value = "查询站点接口")
	@ResponseBody
	@ApiImplicitParams({
			@ApiImplicitParam(name = "stationCode", value = "站点编码", paramType = "query", required = false, dataType = "String") })
	@GetMapping(value = "/queryStation")
	public ResultEntity queryStation(String stationCode) {
		return stationService.queryStation(stationCode);
	}

	@ApiOperation(value = "编辑/新增站点通用接口")
	@ResponseBody
	@PostMapping(value = "/edit")
	public ResultEntity editStation(@RequestBody Station station) {
		return stationService.editStation(station);
	}

	@ApiOperation(value = "查询站点树结构")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "keyName", value = "关键字", paramType = "query", required = false, dataType = "String"),
			@ApiImplicitParam(name = "typeCode", value = "站点类型code", paramType = "query", required = false, dataType = "String"),
			@ApiImplicitParam(name = "isUsedString", value = "是否验收/启用0否1是", paramType = "query", required = false, dataType = "String"),
			@ApiImplicitParam(name = "isMonitoring", value = "是否监控0否1是", paramType = "query", required = false, dataType = "String"),
			@ApiImplicitParam(name = "basinnOrAreaOrCustom", value = "basinn流域area区域type类型,custom自定义control控制级别", paramType = "query", required = true, dataType = "String") })
	@GetMapping(value = "/queryTree")
	public ResultEntity queryStationTree(String basinnOrAreaOrCustom, String keyName, String typeCode,
			String isUsedString, String isMonitoring, HttpServletRequest request) {
		ResultEntity resultEntity = new ResultEntity();
		if (basinnOrAreaOrCustom.equals("basinn")) {
			resultEntity = stationService.queryStationBasinnTree(keyName, typeCode, isUsedString, isMonitoring,
					request);
		} else if (basinnOrAreaOrCustom.equals("area")) {
			resultEntity = stationService.queryStationTree(keyName, typeCode, isUsedString, isMonitoring, request);
		} else if (basinnOrAreaOrCustom.equals("custom")) {
			// userId指的是唯一用户名或者用户id
			resultEntity = stationService.queryCustomStation(request, keyName, typeCode, isUsedString, isMonitoring);
		} else if (basinnOrAreaOrCustom.equals("control")) {
			resultEntity = stationService.queryControlTree(keyName, typeCode, isUsedString, isMonitoring);
		} else if (basinnOrAreaOrCustom.equals("type")) {
			resultEntity = stationService.queryStationAndTypeTree(keyName, typeCode, isUsedString, isMonitoring,
					request);
		} else if ("item".equals(basinnOrAreaOrCustom)) {
			resultEntity = stationService.queryStationAndItemTree(keyName, typeCode, isUsedString, isMonitoring,
					request);
		}
		return resultEntity;
	}

	@ApiOperation(value = "查询站点通用接口")
	@ResponseBody
	@PostMapping(value = "/query")
	public ResultEntity queryStation(@RequestBody(required = false) QueryParamDto queryParamDto) {
		return stationService.queryStation(queryParamDto);
	}

	@ApiOperation(value = "删除站点通用接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "站点ID", paramType = "query", required = true, dataType = "String") })
	@DeleteMapping(value = "/delete")
	public ResultEntity deleteStation(String ids) {
		return stationService.deleteStation(ids);
	}

	@ApiOperation(value = "验证站点通用接口")
	@ResponseBody
	@PostMapping(value = "/validate")
	public ResultEntity validateStation(@RequestBody Station station) {
		return stationService.validateStation(station);
	}

	@ApiOperation(value = "保存自定义站点树接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "groupName", value = "分组名称", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "groupStations", value = "分组内站点id", paramType = "query", required = true, allowMultiple = true, dataType = "String") })
	@PostMapping(value = "/customTree")
	public ResultEntity customStation(String groupName, String[] groupStations) {
		// userId指的是唯一用户名或者用户id
		String userId = "000";
		return stationService.customStation(userId, groupName, groupStations);
	}

	@ApiOperation(value = "删除自定义站点树接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "groupName", value = "分组名称", paramType = "query", required = true, dataType = "String") })
	@DeleteMapping(value = "/deleteCustom")
	public ResultEntity deleteCustom(String groupName) {
		// userId指的是唯一用户名或者用户id
		String userId = "000";
		return stationService.deleteCustom(userId, groupName);
	}

	@ApiOperation(value = "更新自定义站点树接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "groupName", value = "分组名称", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "newName", value = "新的分组名", paramType = "query", required = false, dataType = "String"),
			@ApiImplicitParam(name = "newGroupStations", value = "分组内站点id", paramType = "query", required = false, allowMultiple = true, dataType = "String") })
	@PutMapping(value = "/updateCustom")
	public ResultEntity updateCustom(String groupName, String newName, String[] newGroupStations) {
		// userId指的是唯一用户名或者用户id,根据用户名来绑定自定义站点树
		String userId = "000";
		return stationService.updateCustom(userId, groupName, newName, newGroupStations);
	}

	/**
	 * 所有状态查询
	 * 
	 * @param statisData
	 * @return
	 */
	@ApiOperation("所有状态查询，包括区域、河流以及其下站点")
	@RequestMapping(value = "/holeStatis", method = RequestMethod.GET)
	public ResultEntity realDatasHoleStatis(StatisDataQueryVO statisData, String statusType,
			HttpServletRequest request) {
		List<MainStatisVO> rtnlist = stationService.allCateStatis(statisData, statusType, request);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(rtnlist);
	}

	@ApiOperation("空气站所有状态查询，包括区域、河流以及其下站点")
	@RequestMapping(value = "/holeAirStatis", method = RequestMethod.GET)
	public ResultEntity holeAirStatis(StatisDataQueryVO statisData, String statusType, HttpServletRequest request)
			throws Exception {
		List<MainStatisVO> rtnlist = stationService.allAirCateStatis(statisData, statusType, request);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(rtnlist);
	}

	@ApiOperation("统计站点状态查询")
	@RequestMapping(value = "/packStatonTypeListVP", method = RequestMethod.GET)
	public ResultEntity packStatonTypeListVP(HttpServletRequest request) {
		List<StationTypeListVO> rtnlist = stationService.packStatonTypeListVP(request);
		return new ResultEntity(ResultStatus.OK, Constants.QUERY_SUCCESS).setDataList(rtnlist);
	}

	@ApiOperation("空气站统计站点状态查询")
	@RequestMapping(value = "/packAirStatonTypeListVP", method = RequestMethod.GET)
	public ResultEntity packAirStatonTypeListVP(HttpServletRequest request) throws Exception {
		List<StationTypeListVO> rtnlist = stationService.packAirStatonTypeListVP(request);
		return new ResultEntity(ResultStatus.OK, Constants.QUERY_SUCCESS).setDataList(rtnlist);
	}

	@ApiOperation("查询监测站所有信息")
	@RequestMapping(value = "/stationInfoByMn", method = RequestMethod.GET)
	public ResultEntity queryStationInfoByMn(String mn) {
		return stationService.queryStationInfoByMn(mn);
	}

	@ApiOperation("查询所有启用监测站所有信息")
	@RequestMapping(value = "/queryAllStationInfo", method = RequestMethod.GET)
	public ResultEntity queryAllStationInfo(HttpServletRequest request) {
		return stationService.queryAllStationInfo(request);
	}

	@ApiOperation("查询所有启用空气监测站所有信息")
	@RequestMapping(value = "/queryAllAirStationInfo", method = RequestMethod.GET)
	public ResultEntity queryAllAirStationInfo(HttpServletRequest request) throws Exception {
		return stationService.queryAllAirStationInfo(request);
	}

	@ApiOperation("查询当前站点下的图片信息")
	@RequestMapping(value = "/queryImage", method = RequestMethod.GET)
	public ResultEntity queryImage(String mn) {
		return stationService.queryImage(mn);
	}

	@ApiOperation("新增当前站点图片")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "out外景inside内景build建设", paramType = "query", required = true, dataType = "String") })
	@RequestMapping(value = "/addImage", method = RequestMethod.POST)
	public ResultEntity addImage(@RequestPart(value = "files", required = true) MultipartFile[] files, String mn,
			String type) {
		// out外景inside内景build建设
		if (type.equals("out")) {
			return stationService.addImageOut(files, mn, type);
		} else if (type.equals("inside")) {
			return stationService.addImageInside(files, mn, type);
		} else if (type.equals("build")) {
			return stationService.addImageBuild(files, mn, type);
		} else {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_FAILUE);
		}
	}

	@ApiOperation("删除当前站点图片")
	@RequestMapping(value = "/deleteImage", method = RequestMethod.GET)
	public ResultEntity deleteImage(String path, String mn, String type) {
		// out外景inside内景build建设
		return stationService.deleteImage(path, mn, type);
	}

	@ApiOperation("获取地表水类型")
	@RequestMapping(value = "/querySurfaceWaterTypes", method = RequestMethod.GET)
	public ResultEntity querySurfaceWaterTypes() {
		return stationService.querySurfaceWaterTypes();
	}

	@ApiOperation("获取水质目标级别")
	@RequestMapping(value = "/queryTargetLevel", method = RequestMethod.GET)
	public ResultEntity queryTargetLevel() {
		return stationService.queryTargetLevel();
	}

	@ApiOperation("获取站点功能")
	@RequestMapping(value = "/queryFunction", method = RequestMethod.GET)
	public ResultEntity queryFunction() {
		return stationService.queryFunction();
	}

	@ApiOperation("获取当前用户下站点信息")
	@RequestMapping(value = "/queryByUserId", method = RequestMethod.GET)
	public ResultEntity queryByUserId(HttpServletRequest request) {
		return stationService.queryByUserId(request);
	}

	@ApiOperation("获取站点信息")
	@RequestMapping(value = "/queryStationInfo", method = RequestMethod.GET)
	public ResultEntity queryStation() {
		return stationService.queryStationInfo();
	}

	@ApiOperation("平台查询站点树/用户权限查询站点树")
	@RequestMapping(value = "/queryTreeByPlatform", method = RequestMethod.GET)
	public ResultEntity queryTreeByPlatform(HttpServletRequest request) {
		String platfrom = CookiesUtil.getCookieValue(request, "platform");
		String appCode = CookiesUtil.getCookieValue(request, "appCode");
		return stationService.queryTreeByPlatform(platfrom, appCode);
	}

	@ApiOperation("运维分公司下站点-----运维GIS首页展示")
	@RequestMapping(value = "/queryStationByOfcId", method = RequestMethod.GET)
	public ResultEntity queryStationByOfcId(HttpServletRequest request, String ofcId, String key) {
		return stationService.queryStationByOfcId(request, ofcId, key);
	}

	@ApiOperation("运维分公司站点总数及状态-----运维GIS首页展示")
	@RequestMapping(value = "/queryStationStute", method = RequestMethod.GET)
	public ResultEntity queryStationStute(HttpServletRequest request) {
		return stationService.queryStationStute(request);
	}

	@ApiOperation("宇星运维-----实时数据历史站点信息（只含站点信息不含因子值）")
	@RequestMapping(value = "/queryStationOnly", method = RequestMethod.GET)
	public ResultEntity queryStationOnly(HttpServletRequest request, String typeId,
			com.infore.siteManagement.util.PageObject pageObject) {
		return stationService.queryStationOnly(request, typeId, pageObject);
	}

	@ApiOperation("雄安 查询测站基础数据")
	@RequestMapping(value = "/basicStationInfos", method = RequestMethod.POST)
	public ResultEntity queryBaiscStationInfo(@RequestBody YunlAppInfoParam params) {
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS)
				.setDataList(stationService.queryBaiscStationInfo(params, params.getType()));
	}
	
	@ApiOperation("瑞丰 查询测站基础数据")
	@ResponseBody
	@RequestMapping(value = "/queryBaiscStationInfoById", method = RequestMethod.POST)
	public ResultEntity queryBaiscStationInfoById(@RequestBody String id) {
		return stationService.queryBaiscStationInfoById(id);
	}
	
	@ApiOperation("查询站点绑定人员的id")
	@ResponseBody
	@RequestMapping(value = "/queryUserIdByStationMn", method = RequestMethod.POST)
	public ResultEntity queryUserIdByStationMn(String mn) {
		return stationService.queryUserIdByStationMn(mn);
	}
	
	
}
